<?php

class Department extends CI_Controller {
 
       public function __construct()
    {
        parent::__construct();
        $this->load->model('department_model');
    }
    
    public  function index(){
        if($this->session->userdata('is_logged_in')){
			$this->list_department();
        }else{
        	$this->load->view('admin/login');	
        }
       
 
    }
    public function  list_department(){
        $return_value = $this->department_model->get_department();
        $data['department'] =$return_value['result'];
        $data['message'] =$return_value['message'];
        $data['state'] =$return_value['state'];
        $data['main_content'] = 'admin/department/list';
        $this->load->view('includes/template', $data); 
    }
    
     public function add()
    {
        $this->load->model('department_model');
           date_default_timezone_set('America/Los_Angeles');
        if ($this->input->server('REQUEST_METHOD') === 'POST')
        {
            //form validation
            $this->form_validation->set_rules('dept_id', 'Department ID', 'required');
            $this->form_validation->set_rules('name', 'Name', 'required');
            $this->form_validation->set_error_delimiters('<div class="alert alert-error"><a class="close" data-dismiss="alert">×</a><strong>', '</strong></div>');

            //if the form has passed through the validation
            if ($this->form_validation->run())
            {
                $data_to_store = array(
                    'dept_id' => $this->input->post('dept_id'),
                    'name' => $this->input->post('name'),
                );
               
                $data = $data_to_store;
                
//                $sql = "INSERT INTO Department(dep_id,name) "
//                . "values ('".$data['dept_id']."','".$data['name']."')" ;
//                 $data['info'] =$sql;
//            $data['main_content'] = 'test';
//        $this->load->view('includes/template', $data); 
                
               $return_value = $this->department_model->store_department($data_to_store);
              $data['department'] =$return_value['result'];
               $data['message'] =$return_value['message'];
               $data['state'] =$return_value['state'];
               
               if($return_value['state']== 1){
                    $this->session->set_flashdata('flash_message', 'added');
                    redirect('index.php/department');
               } else {
                    $this->session->set_flashdata('flash_message', 'not_added');
                    redirect('index.php/department/add');
               }
               

            }
        }
        
         $return_value = $this->department_model->get_department();
         $data['department'] =$return_value['result'];
         $data['message'] =$return_value['message'];
         $data['state'] =$return_value['state'];
         $data['main_content'] = 'admin/department/add';
         $this->load->view('includes/template', $data); 

        
    }
    public function update()
    {
           $this->load->model('department_model');
           date_default_timezone_set('America/Los_Angeles');
        //product id 
        $id = $this->uri->segment(3);
        //if save button was clicked, get the data sent via post
        if ($this->input->server('REQUEST_METHOD') === 'POST')
        {
            //form validation
           //form validation
            $this->form_validation->set_rules('dept_id', 'Department ID', 'required');
            $this->form_validation->set_rules('name', 'Name', 'required');
            $this->form_validation->set_error_delimiters('<div class="alert alert-error"><a class="close" data-dismiss="alert">×</a><strong>', '</strong></div>');

            if ($this->form_validation->run())
            {
    
                $data_to_store = array(
                    'dept_id' => $this->input->post('dept_id'),
                    'name' => $this->input->post('name'),
                );
               
                //if the insert has returned true then we show the flash message
               
                
                
//                                $data = $data_to_store;
//                                
//                                $sql = "UPDATE Department
//               SET 
//                    name='".$data['name']."' "
//                . "WHERE dep_id=".$data['dept_id']."";
//                
//      
//                 $data['info'] =$sql;
//            $data['main_content'] = 'test';
//        $this->load->view('includes/template', $data); 
//                                
                $return_value = $this->department_model->update_department($data_to_store);
                
               $data['department'] =$return_value['result'];
               $data['message'] =$return_value['message'];
               $data['state'] =$return_value['state'];
               if($return_value['state']== 1){
                     $this->session->set_flashdata('flash_message', 'updated');
                    redirect('index.php/department');
               } else {
                     $this->session->set_flashdata('flash_message', 'not_updated');
                    redirect('index.php/department/update/'.$data_to_store['dept_id'].'');
               }
               
               
//           $data['info'] =$sql;
//           $data['main_content'] = 'test';
//           $this->load->view('includes/template', $data); 

            }//validation run

        }

        //department data
         $return_value = $this->department_model->get_department_by_id($id);
         $array_department =$return_value['result'];
         $data['department'] = $array_department[0];
         $data['message'] =$return_value['message'];
         $data['state'] =$return_value['state'];
    
      
        //load the view
        $data['main_content'] = 'admin/department/edit';
        $this->load->view('includes/template', $data);            

    }//update
    
     /**
    * Delete product by his id
    * @return void
    */
    public function delete()
    {
        
        $id = $this->uri->segment(3);
        

////                                
          $return_value = $this->department_model->delete_department($id);
               $data['department'] =$return_value['result'];
               $data['message'] =$return_value['message'];
               $data['state'] =$return_value['state'];
               if($return_value['state']== 1){
                     $this->session->set_flashdata('flash_message', 'deleted');
                    redirect('index.php/department');
               } else {
                     $this->session->set_flashdata('flash_message', 'not_deleted');
                    redirect('index.php/department');
               }
    }//edit
    
    
}

